package com.msmall.community.baby.preparer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.common.model.Commend;
import com.msmall.community.dpt.model.Segment;
import com.msmall.community.klg.model.Article;

/**
 * 好孕妈妈模块
 */
public class BabyIndexMotherPreparer implements ViewPreparer{
	
	private static final Log log = LogFactory.getLog(BabyIndexMotherPreparer.class);
	
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			CascadeCollection<Segment> cs = (CascadeCollection<Segment>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_SEGMENT_CASCADECOLLECTION);
			CascadeCollection<Segment> klgSeg = cs.getAllDatas().get(ConstantDB.SEGMENT_ID_NAV);
			
			//妈妈热议
			request.setAttribute("motherKlgHot", CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_COMMEND_MOTHER));
			
			//正文
			Map<Long,Commend> klgCommendSubSegmentImages = (Map<Long,Commend>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_COMMEND_SUBSEGMENT_IMAGES);
			Map<Long, List<Commend>> klgCommendTopNews = (Map<Long, List<Commend>>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_COMMEND_TOP_NEWS);
			Map<Long, List<Article>> hotMap = (Map<Long, List<Article>>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_SUBSEGMENT_HOT);
			Map<Long,Commend> klgCommendImages = (Map<Long,Commend>)CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_KLG_COMMEND_IMAGES);
			
			//好孕妈妈的知识模块
			List<CascadeCollection<Segment>> motherTopSegment = new ArrayList<CascadeCollection<Segment>>();
			request.setAttribute("motherTopSegment", motherTopSegment);
			
			Commend babyIndexMotherImage = null;// 妈妈热议 上面的图片
					
			Map<Long, List<Commend>> motherKlgEachImages = new HashMap<Long, List<Commend>>();//正文每个模块下的2个图片
			request.setAttribute("motherKlgEachImages", motherKlgEachImages);
			
			Map<Long, List<Commend>> motherKlgEachTopNews = new HashMap<Long, List<Commend>>();//正文每个模块下的包含摘要的文章
			request.setAttribute("motherKlgEachTopNews", motherKlgEachTopNews);
			
			Map<Long, List<Article>> motherKlgEachHot = new HashMap<Long, List<Article>>();//正文每个模块下的包含的文章列表
			request.setAttribute("motherKlgEachHot", motherKlgEachHot);
			List<Article> articles = null;
			List<Commend> commends = null;
			for(CascadeCollection<Segment> top : klgSeg.getChildren()){
				if(top.getCurrent().getSegmentid().compareTo(ConstantDB.SEGMENT_ID_KLG_MOTHER_MAX) >0){
					continue;
				}
				motherTopSegment.add(top);
				
				if(klgCommendImages != null && babyIndexMotherImage == null){
					babyIndexMotherImage = klgCommendImages.get(top.getCurrent().getSegmentid());
					request.setAttribute("babyIndexMotherImage",babyIndexMotherImage);
				}
				
				commends = motherKlgEachTopNews.get(top.getCurrent().getSegmentid());
				if(commends == null){
					commends = new ArrayList<Commend>();
					motherKlgEachTopNews.put(top.getCurrent().getSegmentid(), commends);
				}
				if(klgCommendTopNews.get(top.getCurrent().getSegmentid()) != null){
					commends.addAll(klgCommendTopNews.get(top.getCurrent().getSegmentid()));
				}
				
				for(CascadeCollection<Segment> each: top.getChildren()){
					commends = motherKlgEachImages.get(top.getCurrent().getSegmentid());
					if(commends == null){
						commends = new ArrayList<Commend>();
						motherKlgEachImages.put(top.getCurrent().getSegmentid(), commends);
					}
					if(klgCommendSubSegmentImages.get(each.getCurrent().getSegmentid()) != null){
						commends.add(klgCommendSubSegmentImages.get(each.getCurrent().getSegmentid()));
					}
					
					articles = motherKlgEachHot.get(top.getCurrent().getSegmentid());
					if(articles == null){
						articles = new ArrayList<Article>();
						motherKlgEachHot.put(top.getCurrent().getSegmentid(), articles);
					}
					List<Article> tempList = hotMap.get(each.getCurrent().getSegmentid());
					if(tempList != null && tempList.size() >0){
						articles.addAll(tempList.size()>=3?tempList.subList(0, 3):tempList);
					}
				}
			}
		}catch(Exception e){
			log.debug(e);
		}
	}
}

